package ldh.im.base.pojo;

import ldh.im.base.constant.ImUserStatus;
import ldh.im.base.constant.SexEnum;
import ldh.im.base.constant.TerminalType;
import lombok.Data;
import org.apache.commons.lang3.StringUtils;

import java.util.Arrays;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * @author: ldh
 * @date: 2021-06-06 10:17:42
 * @Description: 用户
 */

@Data
public class ImUser  {


	/**
	 * 主键
	 */
	private Long id;

	/**
	 * 登录码
	 */
	private String code;

	private String userName;

	/**
	 * 登录密码
	 */
	private String password;

	/**
	 * 租户id
	 */
	private String tenantCode;
	private Tenant tenant;

	private String terminalTypes;
	private Set<TerminalType> terminalTypeSet;

	/**
	 * 部门id
	 */
	private String departmentCode;

	/**
	 * 手机号
	 */
	private String mobile;

	/**
	 * 头像
	 */
	private String headImg;

	/**
	 * 座右铭
	 */
	private String motto;

	/**
	 * 性别
	 */
	private SexEnum sex;

	/**
	 * 是否可用
	 */
	private ImUserStatus status;

	private String ext1;
	private String ext2;
	private Character firstLetter;

	private Date lockTime;  // 锁定时间， 在这个时间类都被锁定
	private Integer loginErrorNum; // 登录失败次数

	/**
	 * 上次登录时间
	 */
	private Date loginTime;
	private Date createTime;

	private Long version;
	/**
	 * 更新时间
	 */
	private Date updateTime;
	private Date clientUpdateTime;

	public Set<TerminalType> getTerminalTypeSet() {
		if (StringUtils.isNotBlank(this.getTerminalTypes()) && terminalTypeSet != null) {
			terminalTypeSet = Arrays.stream(terminalTypes.split(",")).filter(s-> !s.trim().equals("")).map(s->TerminalType.valueOf(s)).collect(Collectors.toSet());
		}
		return terminalTypeSet;
	}

	public void addTerminalType(TerminalType terminalType) {
		Set<TerminalType> set = getTerminalTypeSet();
		set = set == null ? new HashSet<>() : set;
		set.add(terminalType);
		terminalTypes = set.stream().map(e->e.name()).collect(Collectors.joining(","));
	}
	
}

